<template>
	<view class="register-container">
		<view class="form-header">
			<u-icon name="arrow-left" size="40" @click="goBack"></u-icon>
			<text class="title">注册账号</text>
		</view>
		
		<view class="register-form">
			<u-form :model="form" ref="uForm">
				<u-form-item label="用户名" prop="username">
					<u-input v-model="form.username" placeholder="请输入用户名" />
				</u-form-item>
				<u-form-item label="密码" prop="password">
					<u-input v-model="form.password" type="password" placeholder="请输入密码" />
				</u-form-item>
				<u-form-item label="确认密码" prop="confirmPassword">
					<u-input v-model="form.confirmPassword" type="password" placeholder="请再次输入密码" />
				</u-form-item>
				<u-form-item label="邮箱" prop="email">
					<u-input v-model="form.email" placeholder="请输入邮箱地址" />
				</u-form-item>
				<u-form-item label="验证码" prop="code">
					<view class="code-container">
						<u-input v-model="form.code" placeholder="请输入验证码" />
						<u-button 
							type="primary" 
							size="mini" 
							:disabled="countdown > 0"
							@click="sendCode"
						>
							{{countdown > 0 ? `${countdown}s后重发` : '获取验证码'}}
						</u-button>
					</view>
				</u-form-item>
			</u-form>
		</view>
		
		<view class="register-buttons">
			<u-button type="primary" @click="handleRegister">注册</u-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					username: '',
					password: '',
					confirmPassword: '',
					email: '',
					code: ''
				},
				countdown: 0,
				rules: {
					username: [{
						required: true,
						message: '请输入用户名',
						trigger: ['blur', 'change']
					}, {
						min: 3,
						max: 20,
						message: '用户名长度在3-20个字符之间',
						trigger: ['blur', 'change']
					}],
					password: [{
						required: true,
						message: '请输入密码',
						trigger: ['blur', 'change']
					}, {
						min: 6,
						message: '密码长度不能少于6个字符',
						trigger: ['blur', 'change']
					}],
					confirmPassword: [{
						required: true,
						message: '请再次输入密码',
						trigger: ['blur', 'change']
					}, {
						validator: (rule, value, callback) => {
							if (value !== this.form.password) {
								callback(new Error('两次输入的密码不一致'));
							} else {
								callback();
							}
						},
						trigger: ['blur', 'change']
					}],
					email: [{
						required: true,
						message: '请输入邮箱地址',
						trigger: ['blur', 'change']
					}, {
						type: 'email',
						message: '请输入正确的邮箱地址',
						trigger: ['blur', 'change']
					}],
					code: [{
						required: true,
						message: '请输入验证码',
						trigger: ['blur', 'change']
					}]
				}
			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			goBack() {
				uni.navigateBack();
			},
			sendCode() {
				this.$refs.uForm.validateField('email', valid => {
					if (valid.length === 0) {
						// TODO: 发送验证码
						console.log('发送验证码到邮箱:', this.form.email);
						this.startCountdown();
					}
				});
			},
			startCountdown() {
				this.countdown = 60;
				const timer = setInterval(() => {
					this.countdown--;
					if (this.countdown <= 0) {
						clearInterval(timer);
					}
				}, 1000);
			},
			handleRegister() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						// TODO: 实现注册逻辑
						console.log('注册表单', this.form);
					}
				});
			}
		}
	}
</script>

<style lang="scss">
.register-container {
	padding: 40rpx;
	min-height: 100vh;
	background-color: #fff;
	
	.form-header {
		display: flex;
		align-items: center;
		margin-bottom: 60rpx;
		
		.title {
			font-size: 36rpx;
			font-weight: bold;
			margin-left: 30rpx;
		}
	}
	
	.register-form {
		margin-top: 40rpx;
	}
	
	.code-container {
		display: flex;
		align-items: center;
		
		.u-button {
			margin-left: 20rpx;
			width: 200rpx;
		}
	}
	
	.register-buttons {
		margin-top: 60rpx;
	}
}
</style>

